package com.lvy.hczn.front.business.service.impl;

import java.nio.charset.StandardCharsets;
import java.util.List;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lvy.hczn.front.business.mapper.WarehouseBoxOutboundMapper;
import com.lvy.hczn.front.business.service.WarehouseBoxOutboundService;
import com.lvy.hczn.front.common.constant.MqConstants;
import com.lvy.hczn.front.common.exception.UtilException;
import com.lvy.hczn.front.common.utils.DateUtils;
import com.lvy.hczn.front.system.domain.business.WarehouseBoxOutbound;
import com.lvy.hczn.mq.rocket.ParamInfo;
import com.lvy.hczn.mq.rocket.ProducerUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lvy.hczn.front.common.core.text.Convert;

/**
 * 货箱出库单Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-23
 */
@Service
@Slf4j
public class WarehouseBoxOutboundServiceImpl extends ServiceImpl<WarehouseBoxOutboundMapper, WarehouseBoxOutbound> implements WarehouseBoxOutboundService {
    @Autowired
    private WarehouseBoxOutboundMapper warehouseBoxOutboundMapper;

    @Autowired
    private ProducerUtil producerUtil;

    /**
     * 查询货箱出库单
     *
     * @param id 货箱出库单主键
     * @return 货箱出库单
     */
    @Override
    public WarehouseBoxOutbound selectWarehouseBoxOutboundById(String id) {
        return warehouseBoxOutboundMapper.selectWarehouseBoxOutboundById(id);
    }

    /**
     * 查询货箱出库单列表
     *
     * @param warehouseBoxOutbound 货箱出库单
     * @return 货箱出库单
     */
    @Override
    public List<WarehouseBoxOutbound> selectWarehouseBoxOutboundList(WarehouseBoxOutbound warehouseBoxOutbound) {
        return warehouseBoxOutboundMapper.selectWarehouseBoxOutboundList(warehouseBoxOutbound);
    }

    /**
     * 新增货箱出库单
     *
     * @param warehouseBoxOutbound 货箱出库单
     * @return 结果
     */
    @Override
    public int insertWarehouseBoxOutbound(WarehouseBoxOutbound warehouseBoxOutbound) {
        warehouseBoxOutbound.setBoxOutboundCode(IdUtil.fastSimpleUUID());
        ParamInfo<WarehouseBoxOutbound> info = new ParamInfo<>();
        info.setWarehouseCode(warehouseBoxOutbound.getWarehouseCode());
        info.setData(warehouseBoxOutbound);
        try {
            //空箱出库发送到中台，告知仓库出库多少空箱
//            producerUtil.sendAsyncMsg(MqConstants.TOPIC_WAREHOUSE_EMPTY_OUT_ENTRY_BOX, null, JSON.toJSONString(info).getBytes(StandardCharsets.UTF_8), IdUtil.fastSimpleUUID());
        } catch (Exception e) {
            throw new UtilException(e.getMessage());
        }
        return warehouseBoxOutboundMapper.insert(warehouseBoxOutbound);
    }

    /**
     * 修改货箱出库单
     *
     * @param warehouseBoxOutbound 货箱出库单
     * @return 结果
     */
    @Override
    public int updateWarehouseBoxOutbound(WarehouseBoxOutbound warehouseBoxOutbound) {
        return warehouseBoxOutboundMapper.updateById(warehouseBoxOutbound);
    }

    /**
     * 批量删除货箱出库单
     *
     * @param ids 需要删除的货箱出库单主键
     * @return 结果
     */
    @Override
    public int deleteWarehouseBoxOutboundByIds(String ids) {
        if (StrUtil.isEmpty(ids)) {
            throw new UtilException("ID为空");
        }
        String[] arr = Convert.toStrArray(ids);
        for (String id : arr) {
            super.removeById(id);
        }
        return arr.length;
    }

    /**
     * 删除货箱出库单信息
     *
     * @param id 货箱出库单主键
     * @return 结果
     */
    @Override
    public int deleteWarehouseBoxOutboundById(String id) {
        if (StrUtil.isEmpty(id)) {
            throw new UtilException("ID为空");
        }
        super.removeById(id);
        return 1;
    }
}
